###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces voter covariates for agreement x consistency for respondents and candidates
########################################################
# inputs :: /data/data_matrix_scored.Rdata;
#				 :: /data/candidate_matrix.csv


# outputs ::
#        :: issueCrossPressureData.Rdata
#				 :: issueCrossPressureDataIdeological.Rdata

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}


setwd(dirs)
load('data/data_matrix_scored.Rdata')

scored=read.csv('data/candidate_matrix.csv')

###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

#data_matrix=data_matrix[,-c(which(names(data_matrix)=='scores'))]
data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty


# build matrix position1,position2,agree1,agree2,consist1,consist1
cte=as.data.frame(matrix(NA,nrow(candidate_matrix),6))
nms=names(candidate_matrix)
nms1=grep(nms,pattern='i1')
nms2=grep(nms,pattern='i2')
for(i in 1:nrow(candidate_matrix)){
	if(mean(is.na(candidate_matrix[i,nms1]))!=1){
		cte[i,1]=nms[nms1][which(candidate_matrix[i,nms1]==1)]
		cte[i,2]=nms[nms2][which(candidate_matrix[i,nms2]==1)]
	}
}

cte[which(cte[,1]=='i1_lgbt'),3]=-1
cte[which(cte[,2]=='i2_lgbt'),4]=-1

cte[which(cte[,1]=='i1_need'),3]=-1
cte[which(cte[,2]=='i2_need'),4]=-1

cte[which(cte[,1]=='i1_freetrade'),3]=-1
cte[which(cte[,2]=='i2_freetrade'),4]=-1

cte[which(cte[,1]=='i1_righttochoose'),3]=-1
cte[which(cte[,2]=='i2_righttochoose'),4]=-1

cte[which(cte[,1]=='i1_raisetaxes'),3]=-1
cte[which(cte[,2]=='i2_raisetaxes'),4]=-1

cte[which(cte[,1]=='i1_co2emissions'),3]=-1
cte[which(cte[,2]=='i2_co2emissions'),4]=-1

cte[which(cte[,1]=='i1_citizenship'),3]=-1
cte[which(cte[,2]=='i2_citizenship'),4]=-1

cte[which(cte[,1]=='i1_guncontrol'),3]=-1
cte[which(cte[,2]=='i2_guncontrol'),4]=-1

cte[which(cte[,1]=='i1_reducemilitary'),3]=-1
cte[which(cte[,2]=='i2_reducemilitary'),4]=-1

cte[which(cte[,1]=='i1_policing'),3]=-1
cte[which(cte[,2]=='i2_policing'),4]=-1

cte[which(cte[,1]=='i1_marriage'),3]=1
cte[which(cte[,2]=='i2_marriage'),4]=1

cte[which(cte[,1]=='i1_govabuse'),3]=1
cte[which(cte[,2]=='i2_govabuse'),4]=1

cte[which(cte[,1]=='i1_unfairtrade'),3]=1
cte[which(cte[,2]=='i2_unfairtrade'),4]=1

cte[which(cte[,1]=='i1_unbornlives'),3]=1
cte[which(cte[,2]=='i2_unbornlives'),4]=1

cte[which(cte[,1]=='i1_cuttaxes'),3]=1
cte[which(cte[,2]=='i2_cuttaxes'),4]=1

cte[which(cte[,1]=='i1_drilling'),3]=1
cte[which(cte[,2]=='i2_drilling'),4]=1

cte[which(cte[,1]=='i1_bordersecurity'),3]=1
cte[which(cte[,2]=='i2_bordersecurity'),4]=1

cte[which(cte[,1]=='i1_gunrights'),3]=1
cte[which(cte[,2]=='i2_gunrights'),4]=1

cte[which(cte[,1]=='i1_strengthenmilitary'),3]=1
cte[which(cte[,2]=='i2_strengthenmilitary'),4]=1

cte[which(cte[,1]=='i1_criminals'),3]=1
cte[which(cte[,2]=='i2_criminals'),4]=1

cte[,5]=0
cte[,6]=0
cte[,5][which(cte[,1]=='i1_lgbt' & candidate_matrix$'ii_lgbt'==1)]=1
cte[,6][which(cte[,2]=='i2_lgbt' & candidate_matrix$'ii_lgbt'==1)]=1

cte[,5][which(cte[,1]=='i1_marriage' & candidate_matrix$'ii_marriage'==1)]=1
cte[,6][which(cte[,2]=='i2_marriage' & candidate_matrix$'ii_marriage'==1)]=1

cte[,5][which(cte[,1]=='i1_need' & candidate_matrix$'ii_need'==1)]=1
cte[,6][which(cte[,2]=='i2_need' & candidate_matrix$'ii_need'==1)]=1

cte[,5][which(cte[,1]=='i1_govabuse' & candidate_matrix$'ii_govabuse'==1)]=1
cte[,6][which(cte[,2]=='i2_govabuse' & candidate_matrix$'ii_govabuse'==1)]=1

cte[,5][which(cte[,1]=='i1_reducemilitary' & candidate_matrix$'ii_reducemilitary'==1)]=1
cte[,6][which(cte[,2]=='i2_reducemilitary' & candidate_matrix$'ii_reducemilitary'==1)]=1

cte[,5][which(cte[,1]=='i1_strengthenmilitary' & candidate_matrix$'ii_strengthenmilitary'==1)]=1
cte[,6][which(cte[,2]=='i2_strengthenmilitary' & candidate_matrix$'ii_strengthenmilitary'==1)]=1

cte[,5][which(cte[,1]=='i1_raisetaxes' & candidate_matrix$'ii_raisetaxes'==1)]=1
cte[,6][which(cte[,2]=='i2_raisetaxes' & candidate_matrix$'ii_raisetaxes'==1)]=1

cte[,5][which(cte[,1]=='i1_cuttaxes' & candidate_matrix$'ii_cuttaxes'==1)]=1
cte[,6][which(cte[,2]=='i2_cuttaxes' & candidate_matrix$'ii_cuttaxes'==1)]=1

cte[,5][which(cte[,1]=='i1_co2emissions' & candidate_matrix$'ii_co2emissions'==1)]=1
cte[,6][which(cte[,2]=='i2_co2emissions' & candidate_matrix$'ii_co2emissions'==1)]=1

cte[,5][which(cte[,1]=='i1_drilling' & candidate_matrix$'ii_drilling'==1)]=1
cte[,6][which(cte[,2]=='i2_drilling' & candidate_matrix$'ii_drilling'==1)]=1

cte[,5][which(cte[,1]=='i1_freetrade' & candidate_matrix$'ii_freetrade'==1)]=1
cte[,6][which(cte[,2]=='i2_freetrade' & candidate_matrix$'ii_freetrade'==1)]=1

cte[,5][which(cte[,1]=='i1_unfairtrade' & candidate_matrix$'ii_unfairtrade'==1)]=1
cte[,6][which(cte[,2]=='i2_unfairtrade' & candidate_matrix$'ii_unfairtrade'==1)]=1

cte[,5][which(cte[,1]=='i1_righttochoose' & candidate_matrix$'ii_righttochoose'==1)]=1
cte[,6][which(cte[,2]=='i2_righttochoose' & candidate_matrix$'ii_righttochoose'==1)]=1

cte[,5][which(cte[,1]=='i1_unbornlives' & candidate_matrix$'ii_unbornlives'==1)]=1
cte[,6][which(cte[,2]=='i2_unbornlives' & candidate_matrix$'ii_unbornlives'==1)]=1

cte[,5][which(cte[,1]=='i1_guncontrol' & candidate_matrix$'ii_guncontrol'==1)]=1
cte[,6][which(cte[,2]=='i2_guncontrol' & candidate_matrix$'ii_guncontrol'==1)]=1

cte[,5][which(cte[,1]=='i1_gunrights' & candidate_matrix$'ii_gunrights'==1)]=1
cte[,6][which(cte[,2]=='i2_gunrights' & candidate_matrix$'ii_gunrights'==1)]=1

cte[,5][which(cte[,1]=='i1_citizenship' & candidate_matrix$'ii_citizenship'==1)]=1
cte[,6][which(cte[,2]=='i2_citizenship' & candidate_matrix$'ii_citizenship'==1)]=1

cte[,5][which(cte[,1]=='i1_bordersecurity' & candidate_matrix$'ii_bordersecurity'==1)]=1
cte[,6][which(cte[,2]=='i2_bordersecurity' & candidate_matrix$'ii_bordersecurity'==1)]=1

cte[,5][which(cte[,1]=='i1_policing' & candidate_matrix$'ii_policing'==1)]=1
cte[,6][which(cte[,2]=='i2_policing' & candidate_matrix$'ii_policing'==1)]=1

cte[,5][which(cte[,1]=='i1_criminals' & candidate_matrix$'ii_criminals'==1)]=1
cte[,6][which(cte[,2]=='i2_criminals' & candidate_matrix$'ii_criminals'==1)]=1

names(cte)=c('policy1','policy2','consistency1','consistency2','agree1','agree2')

candidate_matrix=cbind(candidate_matrix,cte)
# do in or out party
#do.in=T
#if(do.in==T){
  invote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
  invote$pty <- ifelse(invote$conjoints==3,0,1)
#} else if(do.in==F){
  outvote <- subset(candidate_matrix,candidate_matrix$conjoints==4|candidate_matrix$conjoints==7)
  outvote$pty <- ifelse(outvote$conjoints==7,0,1)
#}
save(invote,outvote,file='data/issueCrossPressureData.Rdata')

#END

###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces voter covariates for agreement x consistency for respondents and candidates

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}


setwd(dirs)
load('data/data_matrix_scored.Rdata')

scored=read.csv('data/candidate_matrix.csv')


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty


# build matrix position1,position2,agree1,agree2,consist1,consist1
cte=as.data.frame(matrix(NA,nrow(candidate_matrix),6))
nms=names(candidate_matrix)
nms1=grep(nms,pattern='i1')
nms2=grep(nms,pattern='i2')
for(i in 1:nrow(candidate_matrix)){
	if(mean(is.na(candidate_matrix[i,nms1]))!=1){
		cte[i,1]=nms[nms1][which(candidate_matrix[i,nms1]==1)]
		cte[i,2]=nms[nms2][which(candidate_matrix[i,nms2]==1)]
	}
}

cte[which(cte[,1]=='i1_lgbt'),3]=-1
cte[which(cte[,2]=='i2_lgbt'),4]=-1

cte[which(cte[,1]=='i1_need'),3]=-1
cte[which(cte[,2]=='i2_need'),4]=-1

cte[which(cte[,1]=='i1_freetrade'),3]=-1
cte[which(cte[,2]=='i2_freetrade'),4]=-1

cte[which(cte[,1]=='i1_righttochoose'),3]=-1
cte[which(cte[,2]=='i2_righttochoose'),4]=-1

cte[which(cte[,1]=='i1_raisetaxes'),3]=-1
cte[which(cte[,2]=='i2_raisetaxes'),4]=-1

cte[which(cte[,1]=='i1_co2emissions'),3]=-1
cte[which(cte[,2]=='i2_co2emissions'),4]=-1

cte[which(cte[,1]=='i1_citizenship'),3]=-1
cte[which(cte[,2]=='i2_citizenship'),4]=-1

cte[which(cte[,1]=='i1_guncontrol'),3]=-1
cte[which(cte[,2]=='i2_guncontrol'),4]=-1

cte[which(cte[,1]=='i1_reducemilitary'),3]=-1
cte[which(cte[,2]=='i2_reducemilitary'),4]=-1

cte[which(cte[,1]=='i1_policing'),3]=-1
cte[which(cte[,2]=='i2_policing'),4]=-1

cte[which(cte[,1]=='i1_marriage'),3]=1
cte[which(cte[,2]=='i2_marriage'),4]=1

cte[which(cte[,1]=='i1_govabuse'),3]=1
cte[which(cte[,2]=='i2_govabuse'),4]=1

cte[which(cte[,1]=='i1_unfairtrade'),3]=1
cte[which(cte[,2]=='i2_unfairtrade'),4]=1

cte[which(cte[,1]=='i1_unbornlives'),3]=1
cte[which(cte[,2]=='i2_unbornlives'),4]=1

cte[which(cte[,1]=='i1_cuttaxes'),3]=1
cte[which(cte[,2]=='i2_cuttaxes'),4]=1

cte[which(cte[,1]=='i1_drilling'),3]=1
cte[which(cte[,2]=='i2_drilling'),4]=1

cte[which(cte[,1]=='i1_bordersecurity'),3]=1
cte[which(cte[,2]=='i2_bordersecurity'),4]=1

cte[which(cte[,1]=='i1_gunrights'),3]=1
cte[which(cte[,2]=='i2_gunrights'),4]=1

cte[which(cte[,1]=='i1_strengthenmilitary'),3]=1
cte[which(cte[,2]=='i2_strengthenmilitary'),4]=1

cte[which(cte[,1]=='i1_criminals'),3]=1
cte[which(cte[,2]=='i2_criminals'),4]=1

cte[,5]=0
cte[,6]=0
cte[,5][which(cte[,1]=='i1_lgbt' & candidate_matrix$'ii_lgbt'==1)]=1
cte[,6][which(cte[,2]=='i2_lgbt' & candidate_matrix$'ii_lgbt'==1)]=1

cte[,5][which(cte[,1]=='i1_marriage' & candidate_matrix$'ii_marriage'==1)]=1
cte[,6][which(cte[,2]=='i2_marriage' & candidate_matrix$'ii_marriage'==1)]=1

cte[,5][which(cte[,1]=='i1_need' & candidate_matrix$'ii_need'==1)]=1
cte[,6][which(cte[,2]=='i2_need' & candidate_matrix$'ii_need'==1)]=1

cte[,5][which(cte[,1]=='i1_govabuse' & candidate_matrix$'ii_govabuse'==1)]=1
cte[,6][which(cte[,2]=='i2_govabuse' & candidate_matrix$'ii_govabuse'==1)]=1

cte[,5][which(cte[,1]=='i1_reducemilitary' & candidate_matrix$'ii_reducemilitary'==1)]=1
cte[,6][which(cte[,2]=='i2_reducemilitary' & candidate_matrix$'ii_reducemilitary'==1)]=1

cte[,5][which(cte[,1]=='i1_strengthenmilitary' & candidate_matrix$'ii_strengthenmilitary'==1)]=1
cte[,6][which(cte[,2]=='i2_strengthenmilitary' & candidate_matrix$'ii_strengthenmilitary'==1)]=1

cte[,5][which(cte[,1]=='i1_raisetaxes' & candidate_matrix$'ii_raisetaxes'==1)]=1
cte[,6][which(cte[,2]=='i2_raisetaxes' & candidate_matrix$'ii_raisetaxes'==1)]=1

cte[,5][which(cte[,1]=='i1_cuttaxes' & candidate_matrix$'ii_cuttaxes'==1)]=1
cte[,6][which(cte[,2]=='i2_cuttaxes' & candidate_matrix$'ii_cuttaxes'==1)]=1

cte[,5][which(cte[,1]=='i1_co2emissions' & candidate_matrix$'ii_co2emissions'==1)]=1
cte[,6][which(cte[,2]=='i2_co2emissions' & candidate_matrix$'ii_co2emissions'==1)]=1

cte[,5][which(cte[,1]=='i1_drilling' & candidate_matrix$'ii_drilling'==1)]=1
cte[,6][which(cte[,2]=='i2_drilling' & candidate_matrix$'ii_drilling'==1)]=1

cte[,5][which(cte[,1]=='i1_freetrade' & candidate_matrix$'ii_freetrade'==1)]=1
cte[,6][which(cte[,2]=='i2_freetrade' & candidate_matrix$'ii_freetrade'==1)]=1

cte[,5][which(cte[,1]=='i1_unfairtrade' & candidate_matrix$'ii_unfairtrade'==1)]=1
cte[,6][which(cte[,2]=='i2_unfairtrade' & candidate_matrix$'ii_unfairtrade'==1)]=1

cte[,5][which(cte[,1]=='i1_righttochoose' & candidate_matrix$'ii_righttochoose'==1)]=1
cte[,6][which(cte[,2]=='i2_righttochoose' & candidate_matrix$'ii_righttochoose'==1)]=1

cte[,5][which(cte[,1]=='i1_unbornlives' & candidate_matrix$'ii_unbornlives'==1)]=1
cte[,6][which(cte[,2]=='i2_unbornlives' & candidate_matrix$'ii_unbornlives'==1)]=1

cte[,5][which(cte[,1]=='i1_guncontrol' & candidate_matrix$'ii_guncontrol'==1)]=1
cte[,6][which(cte[,2]=='i2_guncontrol' & candidate_matrix$'ii_guncontrol'==1)]=1

cte[,5][which(cte[,1]=='i1_gunrights' & candidate_matrix$'ii_gunrights'==1)]=1
cte[,6][which(cte[,2]=='i2_gunrights' & candidate_matrix$'ii_gunrights'==1)]=1

cte[,5][which(cte[,1]=='i1_citizenship' & candidate_matrix$'ii_citizenship'==1)]=1
cte[,6][which(cte[,2]=='i2_citizenship' & candidate_matrix$'ii_citizenship'==1)]=1

cte[,5][which(cte[,1]=='i1_bordersecurity' & candidate_matrix$'ii_bordersecurity'==1)]=1
cte[,6][which(cte[,2]=='i2_bordersecurity' & candidate_matrix$'ii_bordersecurity'==1)]=1

cte[,5][which(cte[,1]=='i1_policing' & candidate_matrix$'ii_policing'==1)]=1
cte[,6][which(cte[,2]=='i2_policing' & candidate_matrix$'ii_policing'==1)]=1

cte[,5][which(cte[,1]=='i1_criminals' & candidate_matrix$'ii_criminals'==1)]=1
cte[,6][which(cte[,2]=='i2_criminals' & candidate_matrix$'ii_criminals'==1)]=1

names(cte)=c('policy1','policy2','consistency1','consistency2','agree1','agree2')

candidate_matrix=cbind(candidate_matrix,cte)
# do in or out party
#do.in=T
#if(do.in==T){
invote <- subset(candidate_matrix,candidate_matrix$conjoints==1|candidate_matrix$conjoints==6)
outvote <- subset(candidate_matrix,candidate_matrix$conjoints==2|candidate_matrix$conjoints==5)

#conjoints -- dem candidates :: out conjoints == 5,7* || in conjoints == 1,3*
#conjoints -- rep candidates :: out conjoints == 2,4* || in conjoints == 6,8*
#invote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
#outvote <- subset(candidate_matrix,candidate_matrix$conjoints==4|candidate_matrix$conjoints==7)


save(invote,outvote,file='data/issueCrossPressureDataIdeological.Rdata')

#END
#END 6_build_cross_pressure data

# trying to keep everything together in the data
# -- cces_stacked in data/cces_stacked_unmatched.Rdata
# -- voter_matrix in data/voter_matrix.csv
# -- candidate_matrix in data/candidate_matrix.csv (revised)
# -- core_for_ggplot in data/core_for_ggplot_rep_global.csv
# -- core_for_ggplot in data/core_for_ggplot_dem_global.csv (for dems)
# -- core_for_ggplot in data/core_for_ggplot_rep_global.csv (for reps)
# -- all_score,rep_score,dem_score, in data/scoreMats.Rdata
# -- data_matrix (just combining revised candidate_matrix & voter_matrix) in data_matrix_scored.Rdata

# -- indices in indices sophistication_indices.Rdata
# -- irt models to measure latent knowledge and attitudes in posterior1.Rdata and posterior2.Rdata
# --  invote,outvote in data/issueCrossPressureData.Rdata
# --  invote,outvote in data/issueCrossPressureDataIdeological.Rdata
